﻿namespace YdmCodeArtCoreApplication.Tests
{
    using System;
    using System.Threading.Tasks;
    using Abp.TestBase;
    using FFF.Test.DataAccess.Framework;

    public class YdmCodeArtCoreApplicationTestBase : AbpIntegratedTestBase<YdmCodeArtCoreApplicationTest04CFModule>
    {
        public YdmCodeArtCoreApplicationTestBase()
        {
            // this.UsingDbContext(context => new TestDataBuilder(context).Build());
        }

        protected virtual void UsingDbContext(Action<MyDbContext> action)
        {
            using (var context = base.LocalIocManager.Resolve<MyDbContext>())
            {
                action(context);
                context.SaveChanges();
            }
        }

        protected virtual T UsingDbContext<T>(Func<MyDbContext, T> func)
        {
            T result;

            using (var context = base.LocalIocManager.Resolve<MyDbContext>())
            {
                result = func(context);
                context.SaveChanges();
            }

            return result;
        }

        protected virtual async Task UsingDbContextAsync(Func<MyDbContext, Task> action)
        {
            using (var context = this.LocalIocManager.Resolve<MyDbContext>())
            {
                await action(context).ConfigureAwait(false);
                await context.SaveChangesAsync(true).ConfigureAwait(false);
            }
        }

        protected virtual async Task<T> UsingDbContextAsync<T>(Func<MyDbContext, Task<T>> func)
        {
            T result;

            using (var context = this.LocalIocManager.Resolve<MyDbContext>())
            {
                result = await func(context).ConfigureAwait(false);
                context.SaveChanges();
            }

            return result;
        }
    }
}
